package com.jason.controllers;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jason.bean.Role;
import com.jason.bean.UserInfo;
import com.jason.bean.vo.Menu;
import com.jason.dao.UserInfoDao;
import com.jason.service.ResourceService;
import com.jason.service.RoleService;
import com.jason.service.UserInfoService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserInfoService userInfoService;
	@Resource
	private RoleService roleService;
	@Resource
	private ResourceService resourceService;
	@Resource
	private UserInfoDao userInfoDao;

	@PostMapping("/login")
	public String login(UserInfo user, HttpSession session) {
		UserInfo login = userInfoService.login(user);
		if (login != null) {
			session.setAttribute("LOGIN_STATUS", login);
			List<Role> roles = roleService.findByUser(login.getId());
			List<Menu> list = resourceService.findByRoles(roles);
			session.setAttribute("MENUS", list);
			return "index";
		}
		return "login";
	}
	@RequestMapping("/test")
	@ResponseBody
	public Object test() {
		List<Role> roles = roleService.findByUser(1);
		List<Menu> list = resourceService.findByRoles(roles);
		return list;
	}
	@GetMapping("/test/session")
	@ResponseBody
	public Object session(String name,HttpSession session) {
		return session.getAttribute(name);
	}
	@GetMapping("/test/session/logout")
	public Object sessionLogut(String name,HttpSession session) {
		session.invalidate();
		return "session清空成功";
	}

	@GetMapping("/login")
	public String login() {
		return "login";
	}
}
